﻿/// <reference name="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\MicrosoftAjax.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\1049\INIT.debug.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\Smart.core.debug.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\Smart.debug.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\Smart.Runtime.Debug.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\CUI.debug.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\Smart.Ribbon.debug.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\Smart.UI.Dialog.debug.js" />

var saveFileContext;
var saveFileListId;
var saveFileSelection;
var saveFilesNotificationId;
var saveFileXmlClient;

function SaveFiles(listId, actionParam) {
	try {
		if ((ctx != undefined) && (ctx.TotalListItems == null || ctx.TotalListItems <= 0)) {
			alert("Данное представление не содержит файлов!");
			return;
		}

		saveFileContext = Smart.ClientContext.get_current();
		saveFileListId = listId;

		// If files selected - extract their id and process
		var selection = Smart.ListOperation.Selection.getSelectedItems(saveFileContext);

		if (selection !== undefined && selection != null) {
			for (var i = 0; i < selection.length; i++) {
				saveFileSelection[i] = selection[i].id;
			}
		}

		saveFileContext.load(saveFileContext.get_web());

		saveFileContext.executeQueryAsync(Function.createDelegate(this, GetAllFiles), Function.createDelegate(this, OnQueryFailed));
	} catch (e) {
		alert(actionParam + " failed. \n" + e);
	}
}

function GetAllFiles() {
	saveFilesNotificationId = Smart.UI.Notify.addNotification("Определение списка файлов", true);

	var params = window.location.search;

	if (params != null && params != "") {
		params += "&";
	} else {
		params = "?";
	}

	params += "CurrentListID=" + encodeURIComponent(saveFileListId) + "&CurrentSelection=" + encodeURIComponent(saveFileSelection.join(",")) + "&CurrentWebID=" + encodeURIComponent(saveFileContext.get_web().get_id()) + "&CurrentSiteUrl=" + encodeURIComponent(saveFileContext.get_url());

	try {
		saveFileXmlClient = new XMLHttpRequest();

		saveFileXmlClient.onreadystatechange = LoadItems;

		saveFileXmlClient.open('GET', "/_layouts/Ittilan/SharepointFileManager/GetAllFiles.ashx" + params, true);
		saveFileXmlClient.send();
	} catch (e) {
		Smart.UI.Notify.removeNotification(saveFilesNotificationId);

		alert("GetAllFiles failed. \n" + e);
	}
}

//function LoadItems(data) {
function LoadItems() {
	if (saveFileXmlClient.readyState != 4) return;

	var data;

	if (saveFileXmlClient.status == 200) {
		data = saveFileXmlClient.responseText;
	} else {
		OnAllFilesFailed(saveFileXmlClient.statusText + ", " + saveFileXmlClient.responseText);
		return;
	}

	Smart.UI.Notify.removeNotification(saveFilesNotificationId);

	if (data == null || data == "") {
		alert("Данное представление не содержит файлов!");
		return;
	}

	Smart.UI.ModalDialog.showModalDialog({ width: 600, height: 200, url: "/_layouts/Smart.Workflow/ShowFiles.aspx?ids=" + encodeURIComponent(data) });
}

// Error handlers

function OnQueryFailed(sender, args) {
	alert('Context query failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}

function OnAllFilesFailed(data) {
	Smart.UI.Notify.removeNotification(saveFilesNotificationId);

	alert("Get all files failed. \n" + data);
}

function OnLoadItemsFailed(sender, args) {
	Smart.UI.Notify.removeNotification(saveFilesNotificationId);

	alert('Load items failed. \nError:' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
